﻿using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;

namespace FB2er.Tests
{
    [TestClass]
    public class FB2XmlTransformerTest
    {
        private static RunTemplate[] ConvertXML(string xml)
        {
            return new FB2XmlTransformer(
                XmlReader.Create(
                    new StringReader(xml)))
                    .RunTemplate
                    .ToEnumerable()
                    .ToArray();
        }

        [TestMethod]
        public void FB2XmlTransformer_OneText_Converted()
        {
            const string xml = "<book><p>test</p></book>";
            Assert.AreEqual(ConvertXML(xml)[0].Text, "test");
        }

        [TestMethod]
        public void FB2XmlTransformer_TwoText_Converted()
        {
            const string xml = "<book><p>test1</p><p>test2</p></book>";
            Assert.AreEqual(2, ConvertXML(xml).Length);
        }

        [TestMethod]
        public void FB2XmlTransformer_Title_Converted()
        {
            const string xml = "<book><title><p>title</p><p>test</p></title></book>";
            var target = ConvertXML(xml);
            Assert.IsTrue(target[0].Elements.Contains("title"));
            Assert.IsTrue(target[1].Elements.Contains("title"));
        }

        [TestMethod]
        public void FB2XmlTransformer_Notes_IdPropagated()
        {
            const string xml = @"<book><body name=""notes""><section id=""10""><p>1</p><p>test</p></section></body></book>";
            var target = ConvertXML(xml);
            Assert.AreEqual(target[0].Attr("id"), "10");
            Assert.AreEqual(target[1].Attr("id"), "10");
        }

        [TestMethod]
        public void FB2XmlTransformer_LinkInText_ElementIsCarried()
        {
            const string xml = @"<book xmlns:l=""http://www.w3.org/1999/xlink""><p><strong>11<a l:href=""abc@dfe.com"">abc@dfe.com</a>22</strong></p></book>";
            var target = ConvertXML(xml);
            Assert.IsTrue(target[2].Elements.Contains("strong"));
        }

        [TestMethod]
        public void FB2XmlTransformer_EmptyLine_Converted()
        {
            const string xml = "<book><p>line1</p><empty-line/><p>line2</p></book>";
            Assert.AreEqual(1, ConvertXML(xml)[1].NEmptyLines);
        }
    }
}
